package com.mask.im.business.strategy.impl.sendcode;

import com.mask.im.business.model.dto.MobileDTO;
import com.mask.im.business.service.RedisService;
import com.mask.im.business.common.util.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import static com.mask.im.business.common.constant.RedisConstant.PHONE_REGISTER;

@Service("phoneRegisterSendCodeImpl")
public class PhoneRegisterSendCodeImpl extends AbstractSendCode {


	@Autowired
	private RedisService redisService;
	@Autowired
	private TaskExecutor taskExecutor;

	@Override
	public void sendCode(String phone) {
//		//校验邮箱是否正确
//		if (!Validator.isMobile(phone)) {
//			throw new BusinessException("手机格式不正确");
//		}
//		//检测验证码是否过期
//		Boolean aBoolean = redisService.hasKey(PHONE_REGISTER + phone);
//		if (BooleanUtil.isTrue(aBoolean)){
//			throw new BusinessException("验证码仍在有效期内");
//		}
		checkPhoneAndCode(phone,PHONE_REGISTER);
		//生成验证码
		String code = getCode();
		//将验证码异步发送
		taskExecutor.execute(()->{
			MobileDTO mobileDTO = MobileDTO.builder()
					.mobile(phone)
					.code(code).build();
			SMSUtil.sendMessage(mobileDTO);
			//将验证码放入redis
			redisService.set(PHONE_REGISTER+phone,code,5*60);
		});
	}
}
